/**
 * Created by V-XiongXiang on 2017/6/30.
 */
import globalSettingPage  from '../templates/modal_globalsetting_detail.html';
GlobalSettingController.$inject = ['$scope','GlobalSettingService','$uibModal','toastr'];

function GlobalSettingController($scope,GlobalSettingService,$uibModal,toastr){
    $scope.viewModel = {
        query:{
            pageNo:1,
            pageSize:10,
            type:""
        },
        dropDown:{
          types:[{name:"url",code:"url"},{name:"system",code:"system"},{name:"all",code:"all"}]
        },
        tableData:{}
    };

    /**
     *  初始化 GlobalSettingMapping
     * */
    $scope.init = function () {
        getGlobalSettingList($scope);
    };

    /*
    *   编辑 GlobalSettingMapping
    * */
    $scope.edit = function (globalId){
        $uibModal.open({
            animation:true,
            template:globalSettingPage,
            size:'ml',
            controller:['$scope','GlobalSettingService','$uibModalInstance',function ($scope,GlobalSettingService,$uibModalInstance) {
                $scope.viewModel = {
                    pageTitle:"编辑字典项",
                    query:{
                        pageNo:1,
                        pageSize:10
                    },
                    dropDown:{
                        types:[]
                    },
                    globalSetting:{
                        id:globalId,
                        name:"",
                        value:"",
                        valueExt:"",
                        type:"",
                        description:"",
                        createUser:"",
                        createTime:"",
                        updateUser:"",
                        updateTime:""
                    }
                };

                initDropDownList($scope);

                getGlobalSettingMappingById($scope);

                $scope.ok = function () {
                    modifyGlobalSettingMappingByGlobalId($scope,function () {
                        $uibModalInstance.close();
                    });
                };

                $scope.cancel = function () {
                    $uibModalInstance.close();
                };
            }]
        });
    }

    let initDropDownList = function ($scope){
        $scope.viewModel.dropDown.types = getDicTypes();
    }

    let getDicTypes = function () {
        return [{name:"url",code:"url"},{name:"system",code:"system"},{name:"all",code:"all"}];
    }

    /**
     *  获取 GlobalSettingMapping 列表
     * */
    let getGlobalSettingList = function ($scope){
        let formData = {
            pageNo:1,
            pageSize:10,
            type:$scope.viewModel.query.type
        };
        GlobalSettingService.getGlobalSettingList(formData)
        .then(function(data){
            if(data.responseCode == "10000" && data.responseStatus == true){
                $scope.viewModel.tableData = data;
            }
        });
    };

    /*
    *   根据 GlobalId 获取Data
    * */
    let getGlobalSettingMappingById = function ($scope){
        if($scope.viewModel.globalSetting.id == "" ||$scope.viewModel.globalSetting.id == undefined){
            toastr.warning("获取信息异常!");
            return;
        }
        let formData = {
            id:$scope.viewModel.globalSetting.id
        }
        GlobalSettingService.getGlobalSettingMappingById(formData)
            .then(function (data) {
            if(data.responseCode == "10000" && data.responseStatus == true){
                $scope.viewModel.globalSetting = data.body;
            }else{
                toastr.warning("获取数据异常!");
            }
        });
    };

    /*
    *   更新 GlobalSettingMapping Data
    * */
    let modifyGlobalSettingMappingByGlobalId = function ($scope,callback){
        if($scope.viewModel.globalSetting.id =="" || $scope.viewModel.globalSetting.id == undefined){
            toastr.error("主键不能为空");
            return;
        }

        if($scope.viewModel.globalSetting.name == "" || $scope.viewModel.globalSetting.name == undefined){
            toastr.error("请输入名称");
            return;
        }

        if($scope.viewModel.globalSetting.value == "" || $scope.viewModel.globalSetting.value == undefined){
            toastr.error("请输入键值");
            return;
        }

        if($scope.viewModel.globalSetting.valueExt == "" ||$scope.viewModel.globalSetting.valueExt == undefined){
            toastr.error("请输入扩展文本");
            return;
        }

        if($scope.viewModel.globalSetting.type == ""|| $scope.viewModel.globalSetting.type == undefined){
            toastr.error("请选择类型");
            return;
        }

        if($scope.viewModel.globalSetting.description == "" || $scope.viewModel.globalSetting.description == undefined){
            toastr.error("请输入描述");
            return;
        }
      //  console.log($scope.viewModel.globalSetting);

        GlobalSettingService.modifyGlobalSetting($scope.viewModel.globalSetting)
            .then(function (data){
                if(data.responseCode == "10000" && data.responseStatus == true){
                    toastr.success("更新成功","Success");
                    callback();
                }else{
                    toastr.warning("更新失败!");
                    callback();
                }
            });
    };
}

angular.module('controller').controller("GlobalSettingController",GlobalSettingController);
